<template>
  <div>
    <div class="page-title">访客列表</div>
    <v-data-table
      :headers="columns"
      :items="dataList"
      :page.sync="page"
      :items-per-page="pageSize"
      hide-default-footer
      :sort-desc="[false, true]"
      multi-sort
      class="elevation-1"
    >
      <template v-slot:item.url="{ item }">
        <a :href="item.url" target="_blank">{{item.url}}</a>
      </template>
      <template v-slot:item.referer="{ item }">
        <a :href="item.referer" target="_blank"><short-display :width="150" :content="item.referer" /></a>
      </template>
      <template v-slot:item.device="{ item }">
        <short-display :width="250" :content="item.device" />
      </template>
    </v-data-table>
    <pagination
      :page="page"
      :page-size="pageSize"
      :total="total"
      @change="handlePaginationChange"
    />
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';

@Component
export default class AdminVisitor extends Vue {
  protected columns: TypeBase.DataTableColumn[] = [];
  protected dataList: TypeVisitor.ListItem[] = [];
  protected page: number = 1;
  protected pageSize: number = 15;
  protected total: number = 0;
  protected created(): void {
    this.columns = [
      {text: 'ID', value: 'id'},
      {text: 'IP', value: 'ip'},
      {text: '链接', value: 'url'},
      {text: '来源', value: 'referer'},
      {text: '设备', value: 'device'},
      {text: '创建时间', value: 'created_at'},
    ];
    this.getVisitorList();
  }
  protected async getVisitorList(): Promise<void> {
    const params: TypeBase.PaginationParam = {
      page: this.page,
      page_size: this.pageSize,
    };
    const { data: {list, page, page_size, total} } = await this.$api.admin.visitorList
    <TypeBase.PaginationResult<TypeVisitor.ListItem[]>>(params);
    this.dataList = list;
    this.total = total;
  }
  protected handlePaginationChange(page: number, pageSize: number): void {
    this.page = page;
    this.pageSize = pageSize;
    this.getVisitorList();
  }
}
</script>

<style lang="scss" scoped>
  
</style>
